package org.example.service;

import org.example.DAO.FavoriteDao;
import org.example.entity.Favorite;
import org.example.impl.FavoriteDaoImpl;
import org.example.Util.DataSourceUtil;

import java.sql.SQLException;
import java.util.List;

public class FavoriteService {
    private FavoriteDao favoriteDao;

    public FavoriteService() {
        this.favoriteDao = new FavoriteDaoImpl(DataSourceUtil.getDataSource());
    }

    public boolean addFavorite(Favorite favorite) {
        try {
            return favoriteDao.addFavorite(favorite);
        } catch (SQLException e) {
            throw new RuntimeException("添加收藏失败", e);
        }
    }


    public boolean removeFavorite(int userId, int contentId, String contentType) {
        try {
            return favoriteDao.removeFavorite(userId, contentId, contentType);
        } catch (SQLException e) {
            throw new RuntimeException("移除收藏失败", e);
        }
    }


    public boolean isFavorite(int userId, int contentId, String contentType) {
        try {
            return favoriteDao.isFavorite(userId, contentId, contentType);
        } catch (SQLException e) {
            throw new RuntimeException("检查收藏状态失败", e);
        }
    }


    public List<Favorite> getUserFavorites(int userId) {
        try {
            List<Favorite> favorites = favoriteDao.getUserFavorites(userId);
            return favorites;
        } catch (SQLException e) {
            throw new RuntimeException("获取用户收藏失败", e);
        }
    }
}